package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.DialogAdapter;
import com.bocai.eelly.bean.DialogBean;
import com.bocai.eelly.bean.PortBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.ApiManage;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.DateTimePickDialogUtil;
import com.bocai.eelly.utils.DialogUtil;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.TimeUtils;
import com.bocai.eelly.utils.Utils;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * 发布货代询价
 */
public class PostHuoDaiActivity extends BaseActivity implements View.OnClickListener{

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_post_huodai_destination_countries)
    TextView tvPostHuodaiDestinationCountries;
    @Bind(R.id.ll_post_huodai_destination_countries)
    LinearLayout llPostHuodaiDestinationCountries;
    @Bind(R.id.tv_post_huodai_destination_port)
    TextView tvPostHuodaiDestinationPort;
    @Bind(R.id.ll_post_huodai_destination_port)
    LinearLayout llPostHuodaiDestinationPort;
    @Bind(R.id.tv_post_huodai_transport_mode)
    TextView tvPostHuodaiTransportMode;
    @Bind(R.id.ll_post_huodai_transport_mode)
    LinearLayout llPostHuodaiTransportMode;
    @Bind(R.id.et_post_huodai_name)
    EditText etPostHuodaiName;
    @Bind(R.id.et_post_huodai_hs)
    EditText etPostHuodaiHs;
    @Bind(R.id.tv_post_huodai_trade_mode)
    TextView tvPostHuodaiTradeMode;
    @Bind(R.id.ll_post_huodai_trade_mode)
    LinearLayout llPostHuodaiTradeMode;
    @Bind(R.id.tv_post_huodai_commerce_glossary)
    TextView tvPostHuodaiCommerceGlossary;
    @Bind(R.id.ll_post_huodai_commerce_glossary)
    LinearLayout llPostHuodaiCommerceGlossary;
    @Bind(R.id.tv_posst_huodai_package_model)
    TextView tvPosstHuodaiPackageModel;
    @Bind(R.id.ll_post_huodai_package_model)
    LinearLayout llPostHuodaiPackageModel;
    @Bind(R.id.et_post_huodai_package_size)
    EditText etPostHuodaiPackageSize;
    @Bind(R.id.et_post_huodai_weight)
    EditText etPostHuodaiWeight;
    @Bind(R.id.et_post_huodai_num)
    EditText etPostHuodaiNum;
    @Bind(R.id.et_post_huodai_volume)
    EditText etPostHuodaiVolume;
    @Bind(R.id.tv_post_huodai_arrive_time)
    TextView tvPostHuodaiArriveTime;
    @Bind(R.id.ll_post_huodai_arrive_time)
    LinearLayout llPostHuodaiArriveTime;
    @Bind(R.id.tv_post_huodai_open_time)
    TextView tvPostHuodaiOpenTime;
    @Bind(R.id.ll_post_huodai_open_time)
    LinearLayout llPostHuodaiOpenTime;
    @Bind(R.id.tv_post_huodai_pay)
    TextView tvPostHuodaiPay;
    @Bind(R.id.ll_post_huodai_pay)
    LinearLayout llPostHuodaiPay;
    @Bind(R.id.et_post_huodai_remark)
    EditText etPostHuodaiRemark;
    @Bind(R.id.ll_post_huodai_choose_photo)
    LinearLayout llPostHuodaiChoosePhoto;
    @Bind(R.id.tv_post_huodai_commit)
    TextView tvPostHuodaiCommit;
    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_huodai);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }
    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }


    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_post_xunjia, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    private void initEvent() {
        llPostHuodaiPackageModel.setOnClickListener(this);//装箱方式
        llPostHuodaiChoosePhoto.setOnClickListener(this);
        llPostHuodaiArriveTime.setOnClickListener(this);
        llPostHuodaiCommerceGlossary.setOnClickListener(this);//贸易术语
        llPostHuodaiDestinationCountries.setOnClickListener(this);//目的国
        llPostHuodaiDestinationPort.setOnClickListener(this);//目的港口
        llPostHuodaiOpenTime.setOnClickListener(this);
        llPostHuodaiTradeMode.setOnClickListener(this);//贸易方式
        llPostHuodaiPay.setOnClickListener(this);
        llPostHuodaiTransportMode.setOnClickListener(this);//运输方式
        tvPostHuodaiCommit.setOnClickListener(this);
    }


    private PortBean portBean=new PortBean();
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_post_huodai_choose_photo:
                Intent intent=new Intent(this,ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.ll_post_huodai_pay:
                String timeline=Utils.getTimeline();
                String sign=Utils.md5(timeline);
                ApiManage.getPayFrieight(this, timeline, sign, Utils.getUserId(this, "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean baseBean) {
                        if (Utils.checkResultCode(baseBean.getReturn_code()))
                        {
                            List<String> strings=new ArrayList<>();
                            for (int i=0;i<baseBean.getData().size();i++)
                            {
                                strings.add(baseBean.getData().get(i).getTitle());
                            }
                            DialogUtil.showDialog(PostHuoDaiActivity.this, new DialogAdapter(PostHuoDaiActivity.this,strings), new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                        tvPostHuodaiPay.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.ll_post_huodai_open_time:
                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostHuodaiOpenTime);
                break;
            case R.id.ll_post_huodai_arrive_time:
                new DateTimePickDialogUtil(this).dateTimePicKDialog(tvPostHuodaiArriveTime);
                break;
            case R.id.ll_post_huodai_commerce_glossary://贸易术语
                String timeline1=Utils.getTimeline();
                String sign1=Utils.md5(timeline1);
                ApiManage.getMaoyiShuyu(this, timeline1, sign1, Utils.getUserId(this, "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean dialogBean) {
                        if (Utils.checkResultCode(dialogBean.getReturn_code()))
                        {
                            List<String> strings=new ArrayList<>();
                            for (int i=0;i<dialogBean.getData().size();i++)
                            {
                                strings.add(dialogBean.getData().get(i).getTitle());
                            }
                            DialogUtil.showGridViewDialog(PostHuoDaiActivity.this, new DialogAdapter(PostHuoDaiActivity.this,strings), new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                    tvPostHuodaiCommerceGlossary.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.ll_post_huodai_destination_countries://目的国
                String timeline2=Utils.getTimeline();
                String sign2=Utils.md5(timeline2);
                ApiManage.getDestionation(this, timeline2, sign2, Utils.getUserId(this, "id"), new Action1<PortBean>() {
                    @Override
                    public void call(PortBean portBean) {
                        PostHuoDaiActivity.this.portBean=portBean;
                        if (Utils.checkResultCode(portBean.getReturn_code()))
                        {

                            List<String> strings=new ArrayList<>();
                            for (int i=0;i<portBean.getData().size();i++)
                            {
                                strings.add(portBean.getData().get(i).getCountry());
                            }
                            DialogUtil.showDialog(PostHuoDaiActivity.this, new DialogAdapter(PostHuoDaiActivity.this,strings), new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                    tvPostHuodaiDestinationCountries.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.ll_post_huodai_destination_port://目的港口

                if(Utils.isEmpty(tvPostHuodaiDestinationCountries.getText().toString()))
                {
                    T.showShort(this,"请先选择目的国");
                }else{
                    List<String> strings=new ArrayList<>();
                    for (int i=0;i<portBean.getData().size();i++)
                    {
                        if(tvPostHuodaiDestinationCountries.getText().equals(portBean.getData().get(i).getCountry()))
                        {
                            for(int j=0;j<portBean.getData().get(i).getPort().size();j++)
                            {

                                strings.add(portBean.getData().get(i).getPort().get(j).getTitle());
                            }
                            break;
                        }

                    }
                    DialogUtil.showDialog(this, new DialogAdapter(this, strings), new OnItemClickListener() {
                        @Override
                        public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                            tvPostHuodaiDestinationPort.setText(item.toString());
                            dialog.dismiss();
                        }
                    });
                }
                break;
            case R.id.ll_post_huodai_trade_mode://贸易方式
                String timeline3=Utils.getTimeline();
                String sign3=Utils.md5(timeline3);
                ApiManage.getTradeMode(this, timeline3, sign3, Utils.getUserId(this, "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean portBean) {
                        if (Utils.checkResultCode(portBean.getReturn_code()))
                        {

                            List<String> strings=new ArrayList<>();
                            for (int i=0;i<portBean.getData().size();i++)
                            {
                                strings.add(portBean.getData().get(i).getTitle());
                            }
                            DialogUtil.showDialog(PostHuoDaiActivity.this, new DialogAdapter(PostHuoDaiActivity.this,strings), new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                    tvPostHuodaiTradeMode.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.ll_post_huodai_transport_mode://运输方式
                String timeline4=Utils.getTimeline();
                String sign4=Utils.md5(timeline4);
                ApiManage.getTransportMode(this, timeline4, sign4, Utils.getUserId(this, "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean portBean) {
                        if (Utils.checkResultCode(portBean.getReturn_code()))
                        {

                            List<String> strings=new ArrayList<>();
                            for (int i=0;i<portBean.getData().size();i++)
                            {
                                strings.add(portBean.getData().get(i).getTitle());
                            }
                            DialogUtil.showDialog(PostHuoDaiActivity.this, new DialogAdapter(PostHuoDaiActivity.this,strings), new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                    tvPostHuodaiTransportMode.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.ll_post_huodai_package_model://装箱方式
                String timeline5=Utils.getTimeline();
                String sign5=Utils.md5(timeline5);
                ApiManage.getZhuangxiangMode(this, timeline5, sign5, Utils.getUserId(this, "id"), new Action1<DialogBean>() {
                    @Override
                    public void call(DialogBean portBean) {
                        if (Utils.checkResultCode(portBean.getReturn_code()))
                        {

                            List<String> strings=new ArrayList<>();
                            for (int i=0;i<portBean.getData().size();i++)
                            {
                                strings.add(portBean.getData().get(i).getTitle());
                            }
                            DialogUtil.showDialog(PostHuoDaiActivity.this, new DialogAdapter(PostHuoDaiActivity.this,strings), new OnItemClickListener() {
                                @Override
                                public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                                    tvPosstHuodaiPackageModel.setText(item.toString());
                                    dialog.dismiss();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.tv_post_huodai_commit:
                if (files == null || files.length == 0) {
                    T.showShort(this, "请选择照片");
                } else {
                    String destination_countries=tvPostHuodaiDestinationCountries.getText().toString();
                    String destination_port = tvPostHuodaiDestinationPort.getText().toString();
                    String transport_mode = tvPostHuodaiTransportMode.getText().toString();
                    String name = etPostHuodaiName.getText().toString();
                    String HS = etPostHuodaiHs.getText().toString();
                    String trade_mode = tvPostHuodaiTradeMode.getText().toString();
                    String commerce_glossary = tvPostHuodaiCommerceGlossary.getText().toString();
                    String packing_method = tvPosstHuodaiPackageModel.getText().toString();
                    String packing_size=etPostHuodaiPackageSize.getText().toString();
                    String real_weight = etPostHuodaiWeight.getText().toString();
                    String total_box_number = etPostHuodaiNum.getText().toString();
                    String total_volume = etPostHuodaiVolume.getText().toString();
                    String hatch_date = tvPostHuodaiArriveTime.getText().toString();
                    String sailing_date = tvPostHuodaiOpenTime.getText().toString();
                    String pay_freight = tvPostHuodaiPay.getText().toString();
                    if (Utils.isEmpty(destination_countries)) {
                        T.showShort(this, "请选择目的国");
                    } else if (Utils.isEmpty(destination_port)) {
                        T.showShort(this, "请选择目的港口");
                    } else if (Utils.isEmpty(transport_mode)) {
                        T.showShort(this, "请选择运输方式");
                    } else if (Utils.isEmpty(name)) {
                        T.showShort(this, "请输入货物名称");
                    } else if (Utils.isEmpty(HS)) {
                        T.showShort(this, "请输入HS编码");
                    } else if (Utils.isEmpty(trade_mode)) {
                        T.showShort(this, "请选择贸易方式");
                    } else if (Utils.isEmpty(commerce_glossary)) {
                        T.showShort(this, "请输选择贸易术语");
                    } else if (Utils.isEmpty(packing_method)) {
                        T.showShort(this, "请选择装箱方式");
                    } else if (Utils.isEmpty(packing_size)) {
                        T.showShort(this, "请输入装箱尺寸");
                    } else if (Utils.isEmpty(real_weight)) {
                        T.showShort(this, "请输入实称重量");
                    } else if (Utils.isEmpty(total_box_number)) {
                        T.showShort(this, "请输入总箱数");
                    } else if (Utils.isEmpty(total_volume)) {
                        T.showShort(this, "请输入集装箱总体积");
                    } else if (Utils.isEmpty(hatch_date)) {
                        T.showShort(this, "请选择到仓日期");
                    } else if (Utils.isEmpty(sailing_date)) {
                        T.showShort(this, "请选择开仓日期");
                    } else if (Utils.isEmpty(pay_freight)) {
                        T.showShort(this, "请选择付运费方");
                    }  else {
                        Loading.show(this, getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }

    private void commit() {
        String openTime = TimeUtils.dataOne(tvPostHuodaiOpenTime.getText().toString());
        String arriveTime = TimeUtils.dataOne(tvPostHuodaiArriveTime.getText().toString());
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_HUODAI_INQUIRY, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(PostHuoDaiActivity.this,e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(PostHuoDaiActivity.this, response);

                }
            },files,fileKeys,getParams(tvPostHuodaiDestinationCountries.getText().toString(),tvPostHuodaiDestinationPort.getText().toString(),
                    tvPostHuodaiTransportMode.getText().toString(),etPostHuodaiName.getText().toString(),etPostHuodaiHs.getText().toString(),
                    tvPostHuodaiTradeMode.getText().toString(),tvPostHuodaiCommerceGlossary.getText().toString(),
                    tvPosstHuodaiPackageModel.getText().toString(),etPostHuodaiPackageSize.getText().toString(),
                    etPostHuodaiWeight.getText().toString(),etPostHuodaiNum.getText().toString(),etPostHuodaiVolume.getText().toString(),
                    arriveTime,openTime,tvPostHuodaiPay.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public OkHttpClientManager.Param[] getParams(String destination_countries, String destination_port, String transport_mode, String name, String HS, String trade_mode,
                                                 String commerce_glossary, String packing_method, String packing_size, String real_weight, String  total_box_number,
                                                 String total_volume, String hatch_date,String sailing_date,String pay_freight){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(this,"id")),
                new OkHttpClientManager.Param("destination_countries", destination_countries),
                new OkHttpClientManager.Param("destination_port", destination_port),
                new OkHttpClientManager.Param("transport_mode", transport_mode),
                new OkHttpClientManager.Param("name", name),
                new OkHttpClientManager.Param("HS", HS),
                new OkHttpClientManager.Param("trade_mode", trade_mode),
                new OkHttpClientManager.Param("commerce_glossary", commerce_glossary),
                new OkHttpClientManager.Param("packing_method", packing_method),
                new OkHttpClientManager.Param("packing_size", packing_size),
                new OkHttpClientManager.Param("real_weight", real_weight),
                new OkHttpClientManager.Param("total_box_number", total_box_number),
                new OkHttpClientManager.Param("total_volume", total_volume),
                new OkHttpClientManager.Param("hatch_date", hatch_date),
                new OkHttpClientManager.Param("sailing_date", sailing_date),
                new OkHttpClientManager.Param("pay_freight", pay_freight)
        };
    }
}
